package readFile;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.StopWatch;

import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class ReadFile03 {

    public static void main(String[] args) {
        StopWatch stopWatch  = new StopWatch();
        stopWatch.start();
        int count = 0;
        try (Stream<String> stream = Files.lines(Paths.get("D:\\code.txt"), StandardCharsets.UTF_8)){
           stream.filter(s1 -> StringUtils.isNotBlank(s1))
                   .filter(str -> Integer.valueOf(str) > 1)
                   .forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
        stopWatch.stop();
        System.out.println("read all lines spend " + stopWatch.getTotalTimeSeconds()+ " s");
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
        long initMemory = memoryUsage.getInit();
        long usedMemory = memoryUsage.getUsed();
        System.out.println("read line count :" + count);
        System.out.println("init memory :" + initMemory/ (1024 * 1024) + "M");
        System.out.println("used memory :" + usedMemory/ (1024 * 1024) + "M");
    }
}
